<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

jimport( 'joomla.application.component.model' );

/**
 * @package Joomla
 * @subpackage JobBoard
 */
class JAJobBoardModelJACodes extends JModel
{
    var $_data;
    var $_table;
    var $_pagination;
    /**
    * Return the query is used to retrive all row from database
    * @return string The query is used 
    */
    function _buildQuery($filter=null){    
    	$field_id = JRequest::getVar('field_id', 0);
    	$query = "SELECT   s.* ".
				 "FROM 	#__ja_codes AS s ".
				 " WHERE s.field_id='$field_id'";
		if ($filter)
		{
			$filter = explode(',',$filter);
			for ($i=0;$i<count($filter);$i++)
				$filter[$i] = "'".$filter[$i]."'";
			$filter = implode(',',$filter);
			$query.= ' and s.value IN ('.$filter.')';
			
		}
		$query .= ' ORDER BY s.id';
        return $query;
    }
    
    /**
    * Get data list of email
    * @return array  Array of objects contain email data
    */
    function &getData(){
        // load data if it doesnnot already exists
        if(empty($this->_data)){
            $query = $this->_buildQuery();
            $this->_data = $this->_getList($query);
        }
        
        return $this->_data;
    }
    
    /**
    * Get email table instance
    * @return JTable Email table object
    */
    function &_getTable(){
        if($this->_table == null){
        	$this->_table = &JTable::getInstance('ja_codes', 'JobBoardTable');
		}
		return $this->_table;
	}		
	/**
	* Remove email record
	* @return Number of removed records
	*/
	function remove(){
	    $cid = JRequest::getVar('cid',array(), 'post', 'array');
	    $n =  count($cid);
	    JArrayHelper::toInteger( $cid );
	    
	    if($n){
	        $query = "DELETE FROM #__ja_codes WHERE id = ". implode(" OR id = ", $cid);
	        $db = &JFactory::getDBO();
	        $db->setQuery($query);
	        if($db->query()){
				$row = $this->_getTable();
				$row->reorder();
	            return $n;
			}
		}
		
		return -1;
	}
	
	/**
	* Get email item by ID
	* @return Email Table object
	*/
	function getItem(){
		static $item;
		if (isset($item)) {
			return $item;
		}

		$table =& $this->_getTable();

		// Load the current item if it has been defined
		$edit	= JRequest::getVar('edit',true);
		$cid = JRequest::getVar( 'cid', array(0), '', 'array' );
		JArrayHelper::toInteger($cid, array(0));
		
		if ($edit) {
			$table->load($cid[0]);
		}

	    $item = $table;
	    return $item;	    
	}
	
	/**
	* Get email Item limit row
	* @return Email Table object
	*/
	
	function getItems($filter = null){
		global $mainframe, $option;		
		$db = &JFactory::getDBO();
		$query = $this->_buildQuery($filter);		
		$db->setQuery($query);
		
		$items = $db->loadObjectList();
		
		return $items;
	}	
	
	/** 
	* Store email item
	* @param array The post array
	*/
	function store(){
	    // Initialize variables
		$db		=& JFactory::getDBO();
		$row	=& $this->getItem();
		$post	= JRequest::get('request');
		
		if (!$row->bind( $post )) {
			echo "<script> alert('".$row->getError(true)."'); window.history.go(-1); </script>\n";
			return JText::_("Bind data false");
		}
		
		if (!$row->check())
		{			
			return false;
		}
		if (!$row->store())
		{
			echo "<script> alert('".$row->getError(true)."'); window.history.go(-1); </script>\n";
			return JText::_('Store fail');
			//return false;
		}
		$row->reorder();
		$row->checkin();
		return $row->id;
		
	}
}